#pragma once
#include <iostream>
#include <cstdarg>
#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

enum
{
    Debug = 0,
    Info,
    Warning, 
    Error,
    Fatal,

    Scream = 10,
    OneFile,
    ClassFile
};

std::string LevelToString(int level)
{
    switch(level)
    {
        case Debug: return "Debug"; break;
        case Info: return "Info"; break;
        case Warning: return "Warning"; break;
        case Error: return "Error"; break;
        case Fatal: return "Fatal"; break;
        default: return "UnKnow"; break;
    }
}
const int defaultStyle = Scream;
const std::string defaultfilename = "log.";
const std::string dirname = "Log";

class Log
{
public:
    Log()
    :_style(defaultStyle), _filename(defaultfilename)
    {
        mkdir(dirname.c_str(), 0775);
    }

    void Enable(int style)
    {
        _style = style;
    }
    std::string TimeStampExLocalTime()
    {
        const time_t currenttime = time(nullptr);
        struct tm* tm = localtime(&currenttime);
        char buffer[128];
        snprintf(buffer, sizeof buffer - 1, "%d-%d-%d %d_%d_%d", tm->tm_year + 1900, tm->tm_mon + 1,
        tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
        return buffer;
    }
    void WriteToOneFile(const std::string& filename, std::string& message)
    {
        umask(0);
        int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        std::cout << filename << std::endl;
        if(fd < 0) return;
        write(fd, message.c_str(), message.size());
        close(fd);
    }
    void WriteToClassFile(const std::string& file_suffix, std::string& message)
    {
        std::string filename = dirname;
        filename += '/';
        filename += _filename;
        filename += file_suffix;
        WriteToOneFile(filename, message);
    }
    void WriteLog(std::string& file_suffix, std::string& message)
    {
        switch (_style)
        {
        case Scream: std::cout << message; break;
        case OneFile: WriteToClassFile("all", message); break;
        case ClassFile: WriteToClassFile(file_suffix, message); break;
        default: break;
        }
    }
    void LogMessage(int level, const char* format, ...)
    {
        char leftbuffer[1024];
        std::string s_level = LevelToString(level);
        std::string s_currenttime = TimeStampExLocalTime();
        std::string s_id = std::to_string(getpid());
        snprintf(leftbuffer, sizeof leftbuffer - 1, "[%s][%s][%s] ", s_level.c_str(), s_currenttime.c_str(), s_id.c_str());

        char rightbuffer[1024];
        va_list args;
        va_start(args, format);
        vsnprintf(rightbuffer, sizeof rightbuffer - 1, format, args);
        va_end(args);

        std::string loginfo = leftbuffer;
        loginfo += rightbuffer;

        WriteLog(s_level, loginfo);
    }

    ~Log(){}
private:
    int _style;
    std::string _filename;
};

Log lg;

class Conf //它是为了简化用户的使用，同时保证日志系统在程序启动时就准备好
{
public:
    Conf()
    {
        lg.Enable(Scream);
    }
    ~Conf(){}
};
Conf conf;   //初始化日志